#include "LineBuffer.h"
#include <iostream>
LineBuffer :: LineBuffer() :
modified(false)
{

}
void LineBuffer :: setPos(size_t pos)
{
	if( pos >= getPos() )
	{
		half[0] += half[1];
		half[1].erase();
	}

	if( pos < getPos() )
	{
		half[1] = half[0].substr((getCurrentLineSize()-(getCurrentLineSize()-pos)),getCurrentLineSize()-pos)+half[1];
		half[0].resize(pos);
	}
}

void LineBuffer :: addPos(int pos)
{
	std::cerr << getPos() << " " << pos << "\n";
	if(pos > 0 || ((-pos) <= (int)getPos()))
		setPos(getPos()+pos);
}

size_t LineBuffer :: getLineCount()
{
	return 1+alines.size()+blines.size();
}
size_t LineBuffer :: getCurrentLine()
{
	return alines.size();
}
size_t LineBuffer :: editCurrentLine(int line)
{
	return 0;
}


size_t LineBuffer :: getCurrentLineSize(int line)
{
	if(!line)
		return half[0].size()+half[1].size();
	else
	{
		if(line > 0)
		{
			if(line < (int)alines.size())
				return alines[line].size();
			else
				return 0;
		}
		else
		{
			line = -line;
			if(line < (int)blines.size())
				return blines[line].size();
			else
				return 0;
		}
	}
}

size_t LineBuffer :: getPos()
{
	return half[0].size();
}

void LineBuffer :: erasePrevChar()
{
	if(half[0].size())
	{
		half[0].resize(half[0].size()-1);
		modified = true;
	}
}

void LineBuffer :: eraseNextChar()
{
	if(half[1].size())
	{
		half[1] = half[1].substr(1);
		modified = true;
	}

}

void LineBuffer :: erase()
{
	modified=true;
	alines.clear();
	blines.clear();
	half[0].clear();
	half[1].clear();
}

void LineBuffer :: addToLine(std::string add)
{
	half[0] += add;
	modified = true;
}

void LineBuffer :: addToLine(char c)
{
	half[0] += c;
	modified = true;
}

bool LineBuffer :: isModified()
{
	if(modified)
	{
		modified = 0;
		return 1;
	}
	return 0;
}

void LineBuffer :: setLine(std::string val)
{
	half[1].erase();
	half[0] = val;
}

LineBuffer :: operator std::string()
{
	return half[0]+half[1];
}
std::string LineBuffer :: str()
{
	return half[0]+half[1];
}
LineBuffer :: ~LineBuffer()
{

}

